import {
  request
} from "../../request/index";
import {
  getUserProfile,
  login,
  getOpenid
} from "../../utils/asyncWx";

Page({

  data: {
    userInfo: {
      openid: null,
      address: {},
      cart: {},
      collect: [],
    }
  },

  async newGetInfo(e) {
    const { userInfo } = await getUserProfile('正在获取');
    this.getUserOpenid();
    console.log(userInfo);
    wx.setStorageSync('userinfo', userInfo);
    wx.navigateBack({
      delta: 0, // 回退前 delta(默认为1) 页面
    })
  },

  async getUserOpenid() {

    try {
      const { code } = await login();
      const res = await getOpenid(code);
      const { openid } = res.data;
      this.setData({
        ['userInfo.openid']: openid,
      })
      wx.setStorageSync('userid', openid);
      const res2 = await request({
        url: "/user/",
        data: {
          userid: openid,
        },
        method: 'post'
      });
      //if判断 res2.status
    } catch (error) {}
  },
})